﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using ChineseSchool.Models.Hethong;

namespace ChineseSchool.BLL
{
    public class B_Image
    {
        private PetaPoco.Database db;
        public B_Image()
        {
            db = new PetaPoco.Database("ConnectionString");
            if (ListImage.Count == 0)
            {
                // Uncomment to add initial records
                //InitListImage();
            }
        }

        // Select All
        public DataTable GetAll()
        {
            DataTable dt = new DataTable();
            dt = db.ExecuteReader("EXEC Image_Get_List");
            return dt;
        }

        // Select by ID
        public M_Image Selectby_id(int ImageID)
        {
            var loadmenu = db.SingleOrDefault<M_Image>("SELECT * FROM Image bc WHERE bc.ImageID = " + ImageID + ";");
            db.CloseSharedConnection();
            return loadmenu;
        }

        public bool CheckExist(int ImageID)
        {
            var loadmenu = db.SingleOrDefault<M_Image>("SELECT * FROM Image bc WHERE bc.ImageID = " + ImageID + ";");
            db.CloseSharedConnection();
            if (loadmenu != null)
                return false;
            else return true;
        }

        public List<M_Image> GetBy_MenuID(int MenuID)
        {
            var Image = db.Fetch<M_Image>(@";EXEC Image_GetByMenuId @@MenuId = @0", MenuID);
            return Image;
        }

        public List<M_Image> GetBy_PageID(int PageID)
        {
            var Image = db.Fetch<M_Image>(@";EXEC Image_GetByPageId @@PageId = @0", PageID);
            return Image;
        }

        public List<M_Image> GetBy_ImageTypeID(int ImageTypeID)
        {
            var Image = db.Fetch<M_Image>(@";EXEC Image_GetByNewTypeId @@ImageTypeId = @0", ImageTypeID);
            return Image;
        }

        private void InitListImage()
        {
            //ListImage.Add(new M_Image() { ID = 1, FirstName = "Elmer", LastName = "Fudd" });
            //ListImage.Add(new M_Image() { ID = 2, FirstName = "Bugs", LastName = "Bunny" });
            //ListImage.Add(new M_Image() { ID = 3, FirstName = "Daffy", LastName = "Duck" });
        }

        public int NextId
        {
            get
            {
                int id = 0;
                if (ListImage.Count != 0)
                {
                    id = ListImage.Max(c => c.imageID);
                }
                return ++id;
            }
        }

        public List<M_Image> ListImage
        {
            get
            {
                if (HttpContext.Current.Session["ListImage"] == null)
                {
                    HttpContext.Current.Session["ListImage"] = new List<M_Image>();
                }
                return HttpContext.Current.Session["ListImage"] as List<M_Image>;
            }
        }
    }
}